package org.rc.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class BusinessTool {
    public static String handleMac(String mac){
        if(StringUtils.isEmpty(mac)){
            return null;
        }
        return mac.replaceAll(":","").toUpperCase();
    }

    public static List<String> removeDuplicateAndContained(List<String> datas){
        if(CollectionUtils.isEmpty(datas) || datas.size()==1){
            return datas;
        }
        datas = datas.stream().distinct().collect(Collectors.toList());
        if(datas.size()>1){
            datas.sort(Comparator.comparingInt(String::length));
            List<String> contained = new ArrayList<>();
            for(int i=0;i<datas.size();i++){
                for(int j=i+1;j<datas.size();j++){
                    String name1 = datas.get(i);
                    String name2 = datas.get(j);
                    if(name2.contains(name1)){
                        contained.add(name1);
                    }
                }
            }
            datas.removeAll(contained);
        }
        return datas;
    }
}
